package com.zhou.JavaAndroidFirm.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.KeyEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.zhou.JavaAndroidFirm.R;
import com.zhou.JavaAndroidFirm.adapter.ImageAdapter;
import com.zhou.JavaAndroidFirm.base.BaseActivity;
import com.zhou.JavaAndroidFirm.widget.ViewPagerFixed;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * @author Administrator
 * @date 2018/7/25 0025
 * @des
 */
public class BigImagePagerActivity extends BaseActivity {
    @BindView(R.id.pager)
    ViewPagerFixed pager;
    @BindView(R.id.guideGroup)
    LinearLayout guideGroup;
    @BindView(R.id.rl_rootview)
    RelativeLayout rlRootview;
    public static final String INTENT_IMGURLS = "imgurls";
    public static final String INTENT_POSITION = "position";
    private List<View> guideViewList = new ArrayList<View>();
    private ImageAdapter mAdapter;

    public static void startImagePagerActivity(Context context, List<String> photos, int position) {
        Intent intent = new Intent(context, BigImagePagerActivity.class);
        intent.putStringArrayListExtra(INTENT_IMGURLS, new ArrayList<String>(photos));
        intent.putExtra(INTENT_POSITION, position);
        context.startActivity(intent);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.act_image_pager;
    }

    @Override
    protected void initPresenter() {

    }

    @Override
    protected void initView(Bundle bundle) {

    }

    @Override
    protected void initEvent() {
        //设置透明状态栏
        SetTranslanteBar();

        int startPos = getIntent().getIntExtra(INTENT_POSITION, 0);
        ArrayList<String> imgUrls = getIntent().getStringArrayListExtra(INTENT_IMGURLS);
        mAdapter = new ImageAdapter(BigImagePagerActivity.this, imgUrls);
        pager.setAdapter(mAdapter);

        pager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                //遍历集合，将当前位置的view置为selected状态，其他全部取消
                for (int i = 0; i < guideViewList.size(); i++) {
                    guideViewList.get(i).setSelected(i == position ? true : false);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        pager.setCurrentItem(startPos);
        addGuideView(guideGroup, startPos, imgUrls);

    }

    /**
     * 添加引导view
     *
     * @param guideGroup
     * @param startPos
     * @param imgUrls
     */
    private void addGuideView(LinearLayout guideGroup, int startPos, ArrayList<String> imgUrls) {
        if (null != imgUrls && imgUrls.size() > 0) {
            guideViewList.clear();
            for (int i = 0; i < imgUrls.size(); i++) {
                View view = new View(this);
                view.setBackgroundResource(R.drawable.selector_guide_bg);
                view.setSelected(i == startPos ? true : false);
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(getResources().getDimensionPixelSize(R.dimen.gudieview_width),
                        getResources().getDimensionPixelSize(R.dimen.gudieview_heigh));
                //设置左边距
                layoutParams.setMargins(10, 0, 0, 0);
                guideGroup.addView(view, layoutParams);
                guideViewList.add(view);
            }
        }
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            BigImagePagerActivity.this.finish();
            BigImagePagerActivity.this.overridePendingTransition(R.anim.fade_in,
                    R.anim.fade_out);
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
